package com.mysterymall.Controller.Admin;

import com.mysterymall.Common.Comp.HttpStatus;
import com.mysterymall.Common.Comp.Log;
import com.mysterymall.Common.Utils.Result;
import com.mysterymall.Model.Dto.GoodsDto;
import com.mysterymall.Service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @ClassName com.mysterymall.Controller.Admin
 * @Description
 * @Author HuangWX
 * @Time 2023/11/06  00:20
 */
@RestController
@RequestMapping("/goods")
public class AdminGoodsCont {


    /**
     * 商品服务
     */
    private  final  GoodsService goodsService;

    /**
     * 构造器注入
     * @param goodsService 商品服务
     */
    @Autowired
    public AdminGoodsCont(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    /**
     * 根据商品ID查询商品
     *
     * @param goodsId 商品类别ID
     * @return Result
     */
    @GetMapping("/getGoodsAttribute")
    @Log
    public Result getGoodsAttribute(@RequestParam("goodsId") String goodsId, HttpServletRequest request) {
        return goodsService.getGoodsAttribute(goodsId,request);
    }



    /**
     * 查询所有商品
     * @param current 当前页
     * @param pageSize 每页条数
     * @return Result
     */
    @GetMapping("/getAllGoods")
    @Log
    public Result getAllGoods(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {

        if (current <= 0 || pageSize <= 0) {
            return new Result(HttpStatus.BAD_REQUEST, "message.ParameterError");
        }
        return goodsService.getAllGoods(current, pageSize);
    }


    /**
     * 模糊查询
     * @param goodsName 商品名称
     * @return Result
     */
    @GetMapping("/fuzzyQuery")
    @Log
    public Result fuzzyQuery(@RequestParam("goodsName") String goodsName) {
        return goodsService.fuzzyQuery(goodsName);
    }



    /**
     * 添加商品
     *
     * @param goodsDto 商品信息
     * @return Result
     */
    @PostMapping("/addGoods")
    @Log
    public Result addGoods(@Valid @RequestBody GoodsDto goodsDto) {
        return goodsService.addGoods(goodsDto);
    }


    /**
     * 根据商品ID查询商品详情
     *
     * @param goodsId 商品ID
     * @return Result
     */
    @GetMapping("/getMonomerGoods")
    @Log
    public Result getMonomerGoods(@RequestParam("goodsId") String goodsId) {
        return goodsService.getMonomerGoods(goodsId);
    }

    /**
     * 修改商品
     *
     * @param goodsDto 商品信息
     * @return Result
     */
    @PostMapping("/updateGoods")
    @Log
    public Result updateGoods(@RequestBody GoodsDto goodsDto) {
        return goodsService.updateGoods(goodsDto);

    }


    /**
     * 删除商品
     *
     * @param id 商品ID
     * @return Result
     */
    @GetMapping("/deleteGoods")
    @Log
    public Result deleteGoods(@RequestParam("id") String id) {
        return goodsService.deleteGoods(id);
    }

}
